package com.cskaoyan.search.service.Impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.dto.AllItemServiceResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.search.converter.ProductConverter;
import com.cskaoyan.search.entity.ItemDocument;
import com.cskaoyan.search.remote.ProductApiClient;
import com.cskaoyan.search.repository.ProductRepository;
import com.cskaoyan.search.service.InitDataService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * \* User: 余雨健
 * \* Date: 2022/11/3
 * \* Time: 21:38
 **/
@Service
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductRepository productRepository;

    @Autowired
    ProductApiClient productApiClient;

    @Autowired
    ProductConverter productConverter;
    @Override
    public void initItems() {
        AllItemServiceResponse allProductItem = productApiClient.getAllProductItem();
        List<ItemDto> callData = allProductItem.getCallData();
        List<ItemDocument> itemDocuments = productConverter.items2Documnets(callData);
        productRepository.saveAll(itemDocuments);
    }
}
